//登录操作
window.addEventListener("DOMContentLoaded", function () {
    //初始加载高度  -- background
    document.querySelector('.div').style.height = window.innerHeight + 'px';
    let tim;
    window.addEventListener('resize',function(){ //窗体大小改变
        this.clearTimeout(tim);
        // document.querySelector('.div').style.height = window.innerHeight +'px';
        tim = setTimeout(function(){
            document.querySelector('.div').style.height = window.innerHeight +'px';
        },100);
    })

    ////登录角色选择
    let type = 0;
    let form = document.querySelector("#login_form");
    let types = document.querySelectorAll("input[name='type']");
    for (key of types) {
        key.addEventListener('change', function () {
            if (this.value == '0' || this.value == '1') {
                document.querySelector('.bor >.bor1').value = '';
                document.querySelector('.bor >.bor1').setAttribute('placeholder', '管理员账号');

            } else {
                document.querySelector('.bor >.bor1').value = '';
                document.querySelector('.bor >.bor1').setAttribute('placeholder', '学号');
            }
            //确定登录类型
            type = this.value;
        });
    }
    //错误提示行
    let error_span = document.querySelector("#error_span");
    //登录按钮
    let btn_login = document.querySelector('#btn_login');
    // 注册按钮
    let btn_register = document.querySelector('#btn_register');
    //重置按钮
    let btn_repossess = document.querySelector('#btn_repossess');
    //账号 密码
    let username = document.querySelector('#username');
    let password = document.querySelector('#password');
    //记住密码
    let cb_rem = document.querySelector('#cb_rem');
    //登录判断
    let usernameReg = /^[a-zA-Z0-9]{5,18}$/;//.test(username);
    let passwordReg = /^[a-zA-Z0-9]{3,18}$/;//.test(password);
    btn_login.addEventListener('click', function () {
        //
        if (username.value == '' || password.value == '') {
            error_span.innerHTML = '账号密码不能为空！'
        } else {

            //初始格式验证通过
            if (type == 0) { //系统管理员登录
                //
                if ("admin" == username.value && "123" == password.value) {
                    //记住密码？
                    console.log(cb_rem.checked); //=> true / false
                    //设置cookie
                    //document.cookie =`loginType = 0;username = ${username.value};password = ${password.value}`;

                    //
                    //
                    cookie_rem(0);

                    //载入系统管理员界面
                    location.assign('./welcome.html')
                } else {
                    //
                    error_span.innerHTML = '账号或密码错误！'
                }
            } else if (type == 1) { // 宿舍管理员登录
                //正则匹配
                if (usernameReg.test(username.value) == false || passwordReg.test(username.value) == false) {
                    error_span.innerHTML = '账号或密码格式错误！'
                } else {
                    //进一步判断账户密码
                    //
                    cookie_rem(1);
                    //载入xxx界面
                    location.assign('./welcome.html')
                }

            } else if (type == 2) { //学生登录
                //正则匹配
                if (usernameReg.test(username.value) == false || passwordReg.test(username.value) == false) {
                    error_span.innerHTML = '账号或密码格式错误！'
                } else {
                    //进一步判断账户密码
                    //
                    cookie_rem(2);
                    //载入xxx界面
                    location.assign('./welcome.html')
                }
            }
        }
    });

    //注册
    btn_register.addEventListener('click', function(e) {
        //
        error_span.innerHTML = '非账户注册时间段，具体通知请联系管理人员！'
    })
    //设置 cookie
    function cookie_rem(type) {
        //密码 cookie
        if (cb_rem.checked == true) {
            //记住密码
            setCookie(username.value, password.value, 2);
        }
        if (cb_rem.checked == false) {
            //不记住密码
            setCookie(username.value, '', 2);
        }
        //
        function setCookie(name, value, day) {
            var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);		//encodeURIComponent
            if (day > 0) {
                var myDate = new Date();
                // console.log(myDate.getDate());
                myDate.setDate(myDate.getDate() + day);
                cookieText += ";expires=" + myDate;
            }
            document.cookie = cookieText;
        }

        //登录类型cookie
        document.cookie = `type =${type},${username.value}`;

    }

    //通过已记住密码的账户自动填充密码
    username.addEventListener('focus', function (e) {
        // console.log(this.value);
        if (!getCookie(this.value) == '') {
            password.value = getCookie(this.value);
            // console.log(getCookie(this.value));
        }
        //
        username.addEventListener('input', function (e) {
            // console.log(this.value);
            if (!getCookie(this.value) == '') {
                password.value = getCookie(this.value);
                // console.log(getCookie(this.value));
            }
            else {
                password.value = '';
            }
        })
    })
    

    //
    //获取cookie  用户名 对应密码
    function getCookie(name) {
        var i = document.cookie.indexOf(name);
        if (i != -1) {
            var start = document.cookie.indexOf('=');
            var end = document.cookie.indexOf(";", i);
            if (end == -1) {
                end = document.cookie.length;
            }
            //
            let uname = document.cookie.substring(0, start)
            if (name == uname) {
                return document.cookie.substring(start + 1, end);
            }

        }
    }


    //重置
    btn_repossess.addEventListener('click', function () {
        // username.value='admin';
        // password.value ='123';
        // error_span.innerHTML = '';
        location.reload();
    });












});
//